package com.xusheng.cli.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xusheng.cli.common.base.MyPage;
import com.xusheng.cli.common.base.MyServiceImpl;
import com.xusheng.cli.entity.dictionaryModule.Dictionary;
import com.xusheng.cli.mapper.DictionaryMapper;
import com.xusheng.cli.service.DictionaryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 字典表 (Dictionary)表服务实现类
 *
 * @author jy
 * @since 2022-01-17 17:33:02
 */
@Service("dictionaryService")
public class DictionaryServiceImpl extends MyServiceImpl<DictionaryMapper, Dictionary> implements DictionaryService {

    /**
     * 通过父字典code 查询子集
     *
     * @param code 父字典code
     * @return java.lang.Object
     * @author jy
     * @since 2020/11/11 13:47
     */
    @Override
    public List<Dictionary> listDicByParentCode(String code) {
        return baseMapper.listDicByParentCode(code);
    }

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @return 所有数据
     * @author jy
     * @since 2022-01-17 17:33:02
     */
    @Override
    public MyPage<Dictionary> queryByPage(MyPage<Dictionary> page, Dictionary dictionary) {
        return baseMapper.queryByPage(page, dictionary);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean set2Pwd(String code) {
        LambdaUpdateWrapper<Dictionary> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(Dictionary::getCode, "EJMM")
                .set(Dictionary::getAttach1, code);
        return update(updateWrapper);
    }

    @Override
    public String get2Pwd() {
        LambdaQueryWrapper<Dictionary> objectLambdaQueryWrapper = Wrappers.lambdaQuery();
        objectLambdaQueryWrapper.eq(Dictionary::getCode, "EJMM");
        return baseMapper.selectOne(objectLambdaQueryWrapper)
                .getAttach1();
    }

    @Override
    public boolean check2Pwd(String pwd) {
        String old = get2Pwd();
        return DigestUtil.bcryptCheck(pwd, old);

    }
}

